<?php
class process_item
{
	var $ipsclass;
	var $item_ids = array();
	
	function auto_run()
	{
		$this->ipsclass->input['item'] = intval($this->ipsclass->input['item']);
		
		if(is_array($this->ipsclass->input['item_id']))
		{
			foreach($this->ipsclass->input['item_id'] as $item_id => $item_id_checker)
			{
				if($item_id != $item_id_checker)
				{
					continue;
				}
				if(!isset($this->ipsclass->market_class->inventory[$this->ipsclass->input['item']][$item_id]))
				{
					continue;
				}
				
				$this->item_ids[$item_id] = $item_id;
			}
		}
		
		if($this->ipsclass->input['item_id'] == 0 or !is_array($this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]) or count($this->item_ids) == 0)
		{
			$this->ipsclass->boink_it($this->ipsclass->market_class->base_url);
		}
		
		if($this->ipsclass->input['resell_item'])
		{
			$this->resell_item();
		}
		else
		{
			$this->use_item();
		}
	}
	
	function use_item()
	{
		if(in_array($this->ipsclass->member['mgroup'],$this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['limit_use']))
		{
			$this->ipsclass->Error(array('MSG'=>'no_use_item'));
		}
		
		//$this->ipsclass->market_class->load_item($this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]);
		
		if(!$this->ipsclass->market_class->items_loaded[$this->ipsclass->input['item']]->can_run_multiple and count($this->ipsclass->input['item_id']) > 1)
		{
			$this->ipsclass->Error(array('MSG'=>'no_multiple_items'));
		}
		
		if($html = $this->ipsclass->market_class->items_loaded[$this->ipsclass->input['item']]->public_use_form($this->item_ids))
		{
			if(!$this->ipsclass->market_class->items_loaded[$this->ipsclass->input['item']]->input_done($this->item_ids))
			{
				$this->ipsclass->output = $html;
				
				$this->ipsclass->nav[] = $this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['item_name'];
				$this->ipsclass->nav[] = $this->ipsclass->lang['use_item'];
				
				// The return will indicate that the output is needed
				return;
			}
		}
		
		$this->ipsclass->market_class->items_loaded[$this->ipsclass->input['item']]->public_use_item($this->item_ids);
		
		// Remove the used items
		// And log them
		foreach($this->item_ids as $inv_id)
		{
			$this->ipsclass->market_class->items_loaded[$this->ipsclass->input['item']]->_write_log($inv_id,"No message");
		}
		$this->ipsclass->market_class->items_loaded[$this->ipsclass->input['item']]->delete_items($this->item_ids);
		
		// If the item does not redirect?
		$this->ipsclass->market_class->redirect('item_used');
	}
	
	function resell_item()
	{
		if(!$this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['allow_resell'])
		{
			$this->ipsclass->Error(array('MSG'=>'no_resell'));
		}
		
		$logs = array();
		$new_money = 0;
		foreach($this->item_ids as $i => $item_id)
		{
			if($this->ipsclass->market_class->items_loaded[$this->ipsclass->input['item']]->public_on_resold_item($this->ipsclass->market_class->inventory[$this->ipsclass->input['item']][$item_id]))
			{
				$logs[$this->ipsclass->market_class->inventory[$this->ipsclass->input['item']][$item_id]['resell_price']][$this->ipsclass->market_class->inventory[$this->ipsclass->input['item']][$item_id]['price_bought']]++;
				$new_money = $this->ipsclass->numbers->add($new_money,$this->ipsclass->market_class->inventory[$this->ipsclass->input['item']][$item_id]['resell_price']);
			}
			else
			{
				unset($this->item_ids[$i]);
			}
		}
		
		if(count($this->item_ids) == 0)
		{
			$this->ipsclass->boink_it($this->ipsclass->market_class->base_url);
		}
		
		$time = time();
		if($this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['resell_putback'] and $this->ipsclass->numbers->compare($this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['stock'],0) >= 0)
		{
			$this->ipsclass->DB->do_update('market_items', array(
																	'stock' => $this->ipsclass->numbers->add($this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['stock'],count($this->item_ids))
										),"item_id=".$this->ipsclass->input['item']);
			
			$this->ipsclass->market_class->cache_items();
		}
		
		foreach($logs as $resell => $items)
		{
			foreach($items as $price => $quanity)
			{
				$this->ipsclass->DB->do_insert('market_buy_logs',array(
																				'member_id' => $this->ipsclass->member['id'],
																				'method' => 'resell',
																				'item_id' => $this->ipsclass->input['item'],
																				'ip_address' => $this->ipsclass->ip_address,
																				'time_bought' => $time,
																				'quanity' => $quanity,
																				'price_bought' => $price,
																				'resell_price' => $resell,
																				'tax_total' => 0,
																				'category' => $this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['category'],
																				'item_name' => $this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['item_name'],
																				'item_description' => $this->ipsclass->parser->pre_db_parse($this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['item_description']),
																				'icon' => $this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['icon'],
																				'category_name' => $this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$this->ipsclass->input['item']]['category']]['cat_name'],
											)						);
			}
		}
		
		$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$new_money)
					),"id=".$this->ipsclass->member['id']);
		$this->ipsclass->DB->build_and_exec_query(array('delete'=>'market_inventory','where'=>"i_id IN (".implode(", ",$this->item_ids).")"));
		
		$this->ipsclass->market_class->redirect('items_resold');
	}
}
?>